/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the seckill4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.jalonjia.seckill.core.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jalonjia.seckill.common.core.constant.CacheConstants;
import com.jalonjia.seckill.common.core.util.R;
import com.jalonjia.seckill.common.log.annotation.SysLog;
import com.jalonjia.seckill.common.security.util.SecurityUtils;
import com.jalonjia.seckill.core.entity.SeckillActivity;
import com.jalonjia.seckill.core.service.SeckillActivityService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;


/**
 * 秒杀活动表
 *
 * @author 贾晓超
 * @date 2022-05-05 20:56:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/seckillactivity")
@Tag(name = "秒杀活动表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SeckillActivityController {

	private final SeckillActivityService seckillActivityService;

	/**
	 * 分页查询
	 *
	 * @param page            分页对象
	 * @param seckillActivity 秒杀活动表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('core_seckillactivity_get')")
	public R getSeckillActivityPage(Page page, SeckillActivity seckillActivity) {
		if (!SecurityUtils.isAdmin()) seckillActivity.setCreateBy(SecurityUtils.username());
		return R.ok(seckillActivityService.page(page, Wrappers.query(seckillActivity).orderByDesc("create_time")));
	}


	/**
	 * 秒杀活动列表查询
	 *
	 * @return
	 */
	@Operation(summary = "秒杀活动列表查询", description = "秒杀活动列表查询")
	@GetMapping("/dict")
	@PreAuthorize("@pms.hasPermission('core_seckillactivity_get')")
	public R getSeckillActivityDict() {
		return R.ok(seckillActivityService.getActivityDict());
	}

	/**
	 * 通过id查询秒杀活动表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('core_seckillactivity_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(seckillActivityService.getById(id));
	}

	/**
	 * 新增秒杀活动表
	 *
	 * @param seckillActivity 秒杀活动表
	 * @return R
	 */
	@Operation(summary = "新增秒杀活动表", description = "新增秒杀活动表")
	@SysLog("新增秒杀活动表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('core_seckillactivity_add')")
	public R save(@RequestBody SeckillActivity seckillActivity) {
		return R.ok(seckillActivityService.save(seckillActivity));
	}

	/**
	 * 修改秒杀活动表
	 *
	 * @param seckillActivity 秒杀活动表
	 * @return R
	 */
	@Operation(summary = "修改秒杀活动表", description = "修改秒杀活动表")
	@SysLog("修改秒杀活动表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('core_seckillactivity_edit')")
	@CacheEvict(value = CacheConstants.SECKILL_DETAIL, allEntries = true)
	public R updateById(@RequestBody SeckillActivity seckillActivity) {
		return R.ok(seckillActivityService.updateById(seckillActivity));
	}

	/**
	 * 通过id删除秒杀活动表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除秒杀活动表", description = "通过id删除秒杀活动表")
	@SysLog("通过id删除秒杀活动表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('core_seckillactivity_del')")
	@CacheEvict(value = CacheConstants.SECKILL_DETAIL, allEntries = true)
	public R removeById(@PathVariable Long id) {
		if(seckillActivityService.duringActivity(seckillActivityService.getById(id))){
			return R.failed("秒杀进行中");
		}
		return R.ok(seckillActivityService.removeById(id));
	}
}
